package com.example.gulimall.member.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.member.dao.MemberReceiveAddressDao;
import com.example.common.entity.member.MemberReceiveAddressEntity;
import com.example.gulimall.member.service.MemberReceiveAddressService;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MemberReceiveAddressEntity> getAddress(Long memberId) {

        if (Objects.isNull(memberId)) {
            return Collections.emptyList();
        }

        List<MemberReceiveAddressEntity> entities = this.baseMapper.selectList(
                Wrappers.<MemberReceiveAddressEntity>lambdaQuery()
                        .eq(MemberReceiveAddressEntity::getMemberId, memberId)
        );

        if (CollectionUtil.isEmpty(entities)) {
            return Collections.emptyList();
        }
        return entities;
    }

}